<template>
  <a-form
    :model="formSearchParams"
    :style="{ marginBottom: '20px' }"
    layout="inline"
    @submit="doSearch"
  >
    <a-form-item field="userName" label="用户名">
      <a-input
        v-model="formSearchParams.userName"
        placeholder="根据用户名搜索"
      />
    </a-form-item>
    <a-form-item field="userProfile" label="用户简介">
      <a-input
        v-model="formSearchParams.userProfile"
        placeholder="根据用户简介搜索"
      />
    </a-form-item>
    <a-form-item>
      <a-button type="primary" html-type="submit" style="width: 120px">
        搜索
      </a-button>
    </a-form-item>
  </a-form>
  <a-table
    :columns="columns"
    :data="dataList"
    :pagination="{
      showTotal: true,
      pageSize: initsearchParams.pageSize,
      current: initsearchParams.current,
      total,
    }"
    @page-change="onPageChange"
  >
    <!--    插槽可以自己定义表格中渲染的形式  比如用户头像-->
    <template #userAvatar="{ record }">
      <a-image width="80px" :src="record.userAvatar" />
    </template>
    <template #doDelete="{ record }">
      <a-button type="primary" status="danger" @click="doDelete(record)"
        >删除
      </a-button>
    </template>
    <template #createTime="{ record }">
      {{ dayjs(record.createTime).format("YYYY-MM-DD HH:mm:ss") }}
    </template>
  </a-table>
</template>

<script setup lang="ts">
import { ref, watchEffect } from "vue";
import {
  deleteUserUsingPost,
  listUserByPageUsingPost,
  updateUserUsingPost,
} from "@/api/userController";
import API from "@/api";
import message from "@arco-design/web-vue/es/message";
import { dayjs } from "@arco-design/web-vue/es/_utils/date";

const initsearchParams = {
  current: 1,
  pageSize: 10,
};
const showForm = ref(false);
const formSearchParams = ref<API.UserQueryRequest>({});
const searchParams = ref<API.UserQueryRequest>({
  ...initsearchParams,
});
/**
 * 当分页变化时，改变搜索条件，触发数据加载
 * 响应式变量依赖应用来跟踪变化，只有当你重新创建了一个新的变量，watchEffect才能监听到变化
 * @param page
 */
const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const dataList = ref<API.User[]>([]);
const total = ref<number>(0);
const loadDate = async () => {
  const res = await listUserByPageUsingPost(searchParams.value);
  if (res.data.code === 0) {
    dataList.value = res.data.data?.records || [];
    total.value = res.data.data?.total || 0;
  } else {
    message.error("获取数据失败" + res.data.message);
  }
};

watchEffect(() => {
  loadDate();
});

//删除用户
const doDelete = async (record: API.User) => {
  if (record.id == null) {
    return;
  }
  const res = await deleteUserUsingPost({ id: record.id });
  if (res.data.code === 0) {
    loadDate();
  } else {
    message.error("删除数据失败" + res.data.message);
  }
};
/**
 * 搜索
 */
//现在搜索的值应该等于初始搜索的值initsearchParams和表单的值formSearchParams
const doSearch = async () => {
  searchParams.value = {
    ...initsearchParams,
    ...formSearchParams.value,
  };
};
// 表格列配置
const columns = [
  {
    title: "id",
    dataIndex: "id",
  },
  {
    title: "账号",
    dataIndex: "userAccount",
  },
  {
    title: "用户名",
    dataIndex: "userName",
  },
  {
    title: "用户头像",
    dataIndex: "userAvatar",
    slotName: "userAvatar",
  },
  {
    title: "用户简介",
    dataIndex: "userProfile",
  },
  {
    title: "权限",
    dataIndex: "userRole",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
    slotName: "createTime",
  },
  {
    title: "删除",
    slotName: "doDelete",
  },
  {
    title: "修改",
    slotName: "doUpdate",
  },
];
</script>

<style scoped></style>
